package edu.stu.mall.fragment.home

import android.app.Application
import android.util.Log
import androidx.lifecycle.viewModelScope
import edu.mall.base.BaseViewModel
import edu.mall.base.SingleLiveEvent
import edu.stu.mall.repository.Repository
import edu.stu.mall.repository.data.BannerListData
import edu.stu.mall.repository.data.ItemModel
import edu.stu.mall.repository.data.RecommendedProductData
import edu.stu.mall.repository.data.RecommendedProductDataItem
import kotlinx.coroutines.launch

class FragHomeViewModel(application: Application) : BaseViewModel(application) {

//    val list = SingleLiveEvent<List<HomeListItemData>?>()
    var recommendedList = SingleLiveEvent<List<RecommendedProductDataItem>?>()
   var bannerData = SingleLiveEvent<BannerListData?>()
    var classList = SingleLiveEvent<List<ItemModel>?>()

    init {
//        getHomeList()
        getRecommendedProductList()
        getMallBanner()


    }
    fun setInitialData(items: List<ItemModel>) {
        // 直接设置初始数据
        classList.value = items
    }

//    private fun getHomeList() {
//        viewModelScope.launch {
//            val data = Repository.getHomeList("0")
//
//            if(data != null) {
//                list.postValue(data.datas)
//            }
//        }
//    }




    private fun getRecommendedProductList() {
        viewModelScope.launch {
            val data: RecommendedProductData? = Repository.getRecommendedProductList()
            if (data?.isNotEmpty() == true) {
                data.forEach {
                    val name = it.name
//                    Log.e("TAG", "getRecommendedProductList: $name", )
                }
                recommendedList.postValue(data)
            }
        }
    }
    private fun getMallBanner() {
        viewModelScope.launch {
            val data: BannerListData? = Repository.getMallBanner()
//            Log.e("TAG", "getMallBanner: $data", )
//            getMallBanner: BannerListData(advertiseList=[Advertise(clickCount=0,
            bannerData.postValue(data)
        }
    }

}